{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "nbsphinx": "hidden"
   },
   "outputs": [],
   "source": [
    "import open3d as o3d\n",
    "import numpy as np\n",
    "import os\n",
    "import sys\n",
    "\n",
    "# monkey patches visualization and provides helpers to load geometries\n",
    "sys.path.append('..')\n",
    "import open3d_tutorial as o3dtut\n",
    "# change to True if you want to interact with the visualization windows\n",
    "o3dtut.interactive = not \"CI\" in os.environ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# File IO\n",
    "This tutorial shows how basic data structures are read and written by Open3D."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Point cloud\n",
    "The code below reads and writes a point cloud."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Testing IO for point cloud ...\")\n",
    "sample_pcd_data = o3d.data.PCDPointCloud()\n",
    "pcd = o3d.io.read_point_cloud(sample_pcd_data.path)\n",
    "print(pcd)\n",
    "o3d.io.write_point_cloud(\"copy_of_fragment.pcd\", pcd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, Open3D tries to infer the file type by the filename extension. The following point cloud file types are supported:\n",
    "\n",
    "Format   | Description\n",
    "---------|---------------\n",
    "`xyz`    | Each line contains `[x, y, z]`, where `x`, `y`, `z` are the 3D coordinates\n",
    "`xyzn`   | Each line contains `[x, y, z, nx, ny, nz]`, where `nx`, `ny`, `nz` are the normals\n",
    "`xyzrgb` | Each line contains `[x, y, z, r, g, b]`, where `r`, `g`, `b` are in floats of range `[0, 1]`\n",
    "`pts`    | The first line is an integer representing the number of points. The subsequent lines follow one of these formats: `[x, y, z, i, r, g, b]`, `[x, y, z, r, g, b]`, `[x, y, z, i]` or `[x, y, z]`, where `x`, `y`, `z`, `i` are of type `double` and `r`, `g`, `b` are of type `uint8`\n",
    "`ply`    | See [Polygon File Format](http://paulbourke.net/dataformats/ply), the ply file can contain both point cloud and mesh data\n",
    "`pcd`    | See [Point Cloud Data](http://pointclouds.org/documentation/tutorials/pcd_file_format.html)\n",
    "\n",
    "It’s also possible to specify the file type explicitly. In this case, the file extension will be ignored."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mesh\n",
    "The code below reads and writes a mesh."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Testing IO for meshes ...\")\n",
    "knot_data = o3d.data.KnotMesh() \n",
    "mesh = o3d.io.read_triangle_mesh(knot_data.path)\n",
    "print(mesh)\n",
    "o3d.io.write_triangle_mesh(\"copy_of_knot.ply\", mesh)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compared to the point cloud data structure, a mesh has triangles that define the 3D surface.\n",
    "\n",
    "By default, Open3D tries to infer the file type by the filename extension. The following mesh file types are supported:\n",
    "\n",
    "Format          | Description\n",
    "----------------|---------------\n",
    "`ply`           | See [Polygon File Format](http://paulbourke.net/dataformats/ply/), the ply file can contain both point cloud and mesh data\n",
    "`stl`           | See [StereoLithography](http://www.fabbers.com/tech/STL_Format)\n",
    "`obj`           | See [Object Files](http://paulbourke.net/dataformats/obj/)\n",
    "`off`           | See [Object File Format](http://www.geomview.org/docs/html/OFF.html)\n",
    "`gltf`/`glb`    | See [GL Transmission Format](https://github.com/KhronosGroup/glTF/tree/master/specification/2.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Image\n",
    "The code below reads and writes an image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Testing IO for images ...\")\n",
    "image_data = o3d.data.JuneauImage()\n",
    "img = o3d.io.read_image(image_data.path)\n",
    "print(img)\n",
    "o3d.io.write_image(\"copy_of_Juneau.jpg\", img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The size of the image is readily displayed using `print(img)`.\n",
    "\n",
    "Both `jpg` and `png` image files are supported."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Edit Metadata",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
